<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/style/admin.css" media="all">
</head>

<body>

    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">通知消息</div>
                    <div class="layui-card-body">
                        <div style="margin-bottom: 10px;">
                            <div class="layui-input-item" style="float:left;padding-right:10px;">
                                <form class="layui-form">
                                    <div class="layui-inline">
                                        <input id="search-type" type="text" placeholder="请输入消息类型"
                                            class="layui-input layui-input-search">
                                    </div>
                                    <div class="layui-inline">
                                        <input id="search-owner-id" type="text" placeholder="请输入所有人ID"
                                            class="layui-input layui-input-search">
                                    </div>
                                    <div class="layui-inline">
                                        <input id="search-owner-name" type="text" placeholder="请输入所有人姓名"
                                            class="layui-input layui-input-search">
                                    </div>
                                    <div class="layui-inline">
                                        <input id="search-content" type="text" placeholder="请输入内容"
                                            class="layui-input layui-input-search">
                                    </div>
                                    <div class="layui-inline">
                                        <a id="btn-search" class="layui-btn">查找</a>
                                    </div>
                                </form>
                            </div>
                            <div style="float:right;">
                                <a id="btn-add" class="layui-btn">添加</a>
                                <a id="btn-batch-delete" class="layui-btn layui-btn-danger">删除</a>
                                <a id="btn-refresh" class="layui-btn layui-btn-primary">刷新</a>
                            </div>
                            <div style="clear:both;"></div>
                        </div>
                        <table class="layui-hide" id="msg-table" lay-filter="msg-table"></table>
                        <script type="text/html" id="toolbar-operation">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">详情</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/html" id="msg-form">
    <div class="layui-card">
        <div class="layui-card-header">通知消息详情</div>
        <div class="layui-card-body">
            <form class="layui-form" action="#" lay-filter="msg-form">
            <input type="hidden" name="id">
            <div class="layui-form-item">
                <label class="layui-form-label">*消息类型</label>
                <div class="layui-input-block">
                    <input type="text" name="type" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">*所有人ID</label>
                <div class="layui-input-block">
                    <input type="number" name="ownerId" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">*所有人姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="ownerName" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">*标题</label>
                <div class="layui-input-block">
                    <input type="text" name="title" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">*内容</label>
                <div class="layui-input-block">
                    <input type="text" name="content" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">跳转路径</label>
                <div class="layui-input-block">
                    <input type="text" name="path" class="layui-input" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">*创建时间</label>
                <div class="layui-input-block">
                    <input type="text" name="createTime" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="form-submit">确定</button>
            </div>
            </form>
        </div>
    </div>
</script>
    <script src="/layui/layui.js"></script>
    <script>
        layui.config({
            base: '/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table', 'form', 'laydate'], function () {
            var $ = layui.$;
            var table = layui.table;
            var form = layui.form;
            var admin = layui.admin;
            var laydate = layui.laydate;
            var popupIndex;


            table.render({
                elem: '#msg-table',
                url: 'msgs',
                title: '',
                skin: 'line',
                cols: [[
                    { type: 'checkbox' },
                    {
                        title: '序号', width: 60, templet: function (d) {
                            return d.LAY_TABLE_INDEX + 1;
                        }
                    },
                    { field: 'type', title: '消息类型' },
                    { field: 'ownerId', title: '所有人ID' },
                    { field: 'ownerName', title: '所有人姓名' },
                    { field: 'title', title: '标题' },
                    { field: 'content', title: '内容' },
                    { field: 'path', title: '跳转路径' },
                    { field: 'createTime', title: '创建时间' },
                    { fixed: 'right', title: '操作', toolbar: '#toolbar-operation' }
                ]],
                page: true
            });

            function showMsgForm() {
                popupIndex = admin.popupRight({
                    area: '360px',
                    success: function () {
                        var formHtml = $('#msg-form').html();
                        $('#' + this.id).html(formHtml);
                        form.render();
                        laydate.render({
                            elem: '#create-time'
                        });
                    }
                });
                return popupIndex;
            }

            function closePopupForm() {
                if (popupIndex) {
                    layer.close(popupIndex);
                }
            }

            function search() {
                table.reload('msg-table', {
                    where: {
                        type: $('#search-type').val(),
                        ownerId: $('#search-owner-id').val(),
                        ownerName: $('#search-owner-name').val(),
                        content: $('#search-content').val(),
                    },
                    page: {
                        curr: 1
                    }
                });
            }

            $(document).on('keypress', function (e) {
                if (e.which == 13) {
                    search();
                }
            });

            $("#btn-search").on('click', function () {
                search();
            });

            $("#btn-add").on('click', function () {
                popupIndex = showMsgForm();
            });

            $("#btn-refresh").on('click', function () {
                table.reload('msg-table');
                layer.msg('表格已刷新');
            });

            $("#btn-batch-delete").on('click', function () {
                layer.confirm('确定要删除吗?', { icon: 3, title: '提示' }, function (index) {
                    var ids = [];
                    var data = table.checkStatus('msg-table').data;
                    for (let i in data) {
                        ids.push(data[i].id);
                    }
                    $.ajax({
                        url: 'msgs/' + ids.toString(),
                        type: 'DELETE',
                        success: function (result) {
                            if (result.code == 200) {
                                layer.msg('删除成功');
                                table.reload('msg-table');
                            } else {
                                layer.msg('删除失败');
                            }
                        }
                    });
                });
            });

            table.on('tool(msg-table)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('真的删除该记录吗？', function (index) {
                        $.ajax({
                            url: 'msgs/' + data.id,
                            type: 'DELETE',
                            success: function (result) {
                                if (result.code == 200) {
                                    layer.msg('删除成功');
                                    table.reload('msg-table');
                                } else {
                                    layer.msg('删除失败');
                                }
                            }
                        });
                        layer.close(index);
                    });
                } else if (obj.event === 'edit') {
                    showMsgForm();
                    form.val('msg-form', data);
                }
            });

            form.on('submit(form-submit)', function (data) {
                $.ajax({
                    url: 'msgs',
                    type: 'PUT',
                    data: data.field,
                    success: function (result) {
                        if (result.code == 200) {
                            layer.msg('操作成功');
                            table.reload('msg-table');
                        } else {
                            layer.msg('提交失败');
                            console.error('提交失败');
                        }
                    }
                });
                closePopupForm();
                return false;
            });

        });
    </script>
</body>

</html>